﻿using Abp.Domain.Repositories;
using AppFramework.DinosaursParkMes.CodingRuleModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppFramework.DinosaursParkMes.DomainService.CodingRule
{
    public class CodingruleManager: AppFrameworkDomainServiceBase,ICodingruleManager
    {
        private readonly IRepository<Codingrules> _repository;

        public CodingruleManager(IRepository<Codingrules> repository)
        {
            _repository = repository;
        }
        /// <summary>
        /// 查找单个编码规则
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Codingrules GetById(int id)
        {
            var entity = _repository.GetAllList().Where(x => x.Id == id).FirstOrDefault();
            return entity;
        }
        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        public async Task<string> GetCode(string tableName)
        {
            var name = "";
            switch (tableName)
            {
                case "Material":
                    name = "物料管理";
                    break;
                case "CraftRoute":
                    name = "工艺路线";
                    break;
                case "Process":
                    name = "工序";
                    break;
                case "CraftProcess":
                    name = "工艺工序";
                    break;
                case "BillOfMaterial":
                    name = "BOM";
                    break;
                case "Productinfo":
                    name = "生产产品";
                    break;
                case "ProductionPlanning":
                    name = "生产计划";
                    break;
                case "ProductWorkModels":
                    name = "生产工单";
                    break;
                case "MaterialRequisition":
                    name = "生产领料管理";
                    break;
                case "TicketTaskinfo":
                    name = "工单任务";
                    break;
                case "zhandian":
                    name = "站点";
                    break;
                case "Announcement":
                    name = "通知";
                    break;
            }

            return await Code(name);
        }
        private async Task<string> Code(string name)
        {
            //上锁
            lock (this)
            {
                //前往规则表中进行查询
                var entity = _repository.GetAll().Where(x => x.TargetForm == name).FirstOrDefault();
                string codeno = "";
                //前缀+日期的生成
                string code = entity.CodingProd + DateTime.Now.ToString(entity.TimeRule);
                //判断是否到达界值
                if (entity.Current_Number / Convert.ToInt32(9.ToString().PadRight(entity.InitialLong, '9')) == 1)
                {
                    entity.Current_Number = 0;
                }
                //判断是否是第一次进行编号的生成
                if (entity.Current_Number == 0)
                {
                    entity.Current_Number = entity.InitialNumber;
                }
                else
                {
                    entity.Current_Number += entity.StepSize;
                }
                //生成的流水号
                codeno = entity.Current_Number.ToString().PadLeft(entity.InitialLong, '0');
                var m = _repository.Update(entity);
                if (m == null)
                {
                    return "编号生成失败";
                }
                else
                {
                    return code + codeno;
                }
            }
        }
        /// <summary>
        /// 编码规则的新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Codingrules> InsertCodingrule(Codingrules model)
        {
            var entity = ObjectMapper.Map<Codingrules>(model);
            Codingrules coding = await _repository.InsertAsync(entity);
            return coding;
        }
        /// <summary>
        /// 查询全部
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<List<Codingrules>> GetAll()
        {
            return _repository.GetAllListAsync();
            //throw new NotImplementedException();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<Codingrules> Update(Codingrules model)
        {
            return _repository.UpdateAsync(model);
            //throw new NotImplementedException();
        }
    }
}
